package sshj;

import io.SshBean;
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.common.IOUtils;
import net.schmizz.sshj.connection.channel.direct.Session;
import net.schmizz.sshj.sftp.SFTPClient;
import net.schmizz.sshj.transport.verification.PromiscuousVerifier;
import net.schmizz.sshj.xfer.FileSystemFile;
import net.schmizz.sshj.xfer.scp.SCPUploadClient;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Timer;

/**
 * @Author Mu
 * @Date 2020/11/19 16:02
 */
public class SSHJUtil {
    public static boolean uploadFile(String src, String dest, SFTPClient sftpClient){
        try {
            sftpClient.mkdirs(dest);
        } catch (IOException e){
            System.out.println(e);
            System.out.println("upload file mkdir fail");
            return false;
        }
        try {
            long start = System.currentTimeMillis();

            sftpClient.put(src, dest);
            System.out.println("sftp upload file cost is :" + (System.currentTimeMillis() - start));
            return true;
        } catch (IOException e){
            System.out.println(e);
            System.out.println("SSHJUtil upload file fail");
            return false;
        }
    }

    public static boolean uploadFileSFTPTest(SshBean sshBean,String src, String dest){
        try {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SSHClient sshClient = new SSHClient();
            sshClient.addHostKeyVerifier(new PromiscuousVerifier());
            sshClient.connect(sshBean.getIp());
            sshClient.authPassword(sshBean.getUsername(), sshBean.getPassword());
            SFTPClient sftpClient = sshClient.newSFTPClient();
            sftpClient.mkdirs(dest);
            long start = System.currentTimeMillis();
            sftpClient.put(src, dest);
//            sftpClient.size(dest);
            long end = System.currentTimeMillis();
            System.out.println(sshBean.getIp() + "开始传递时间是：" + start + "转换后为：" + dateFormat.format(start));
            System.out.println(sshBean.getIp() + "结束传递时间是：" + end + "转换后为：" + dateFormat.format(end));
            System.out.println(sshBean.getIp() + " sftp upload file cost is :" + (end - start));

            sftpClient.close();
            sshClient.disconnect();
        } catch (IOException e){
            System.out.println(e);
            System.out.println("upload file mkdir fail");
            return false;
        }finally {
        }
        return true;
    }

    public static boolean uploadFileBySCPTest(SshBean sshBean,String src, String dest){
        try {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SSHClient sshClient = new SSHClient();
            sshClient.addHostKeyVerifier(new PromiscuousVerifier());
            sshClient.connect(sshBean.getIp());
            sshClient.authPassword(sshBean.getUsername(), sshBean.getPassword());
            SFTPClient sftpClient = sshClient.newSFTPClient();
            sftpClient.mkdirs(dest);
            long start = System.currentTimeMillis();
            sshClient.newSCPFileTransfer().upload(src, dest);
            long end = System.currentTimeMillis();
            System.out.println(sshBean.getIp() + "开始传递时间是：" + start + "转换后为：" + dateFormat.format(start));
            System.out.println(sshBean.getIp() + "结束传递时间是：" + end + "转换后为：" + dateFormat.format(end));
            System.out.println(sshBean.getIp() + " scp upload file cost is :" + (end - start));
            sftpClient.close();
            sshClient.disconnect();
            return true;
        } catch (IOException e){
            System.out.println(e);
            System.out.println("SSHJUtil upload file fail");
            return false;
        }
    }

    public static boolean uploadFileBySCP(String src, String dest, SCPUploadClient scpClient){
//        try {
//            scpClient.p(dest);
//        } catch (IOException e){
//            System.out.println(e);
//            System.out.println("upload file mkdir fail");
//            return false;
//        }
        try {
            long start = System.currentTimeMillis();
            scpClient.copy(new FileSystemFile(src), dest);
            System.out.println("上传花费时间" + (System.currentTimeMillis() - start));
            return true;
        } catch (IOException e){
            System.out.println(e);
            System.out.println("SSHJUtil upload file fail");
            return false;
        }
    }

    public static boolean chmodFile(String filePath, int perms, SFTPClient sftpClient){
        try {
            sftpClient.chmod(filePath, perms);
            return true;
        } catch (IOException e){
            System.out.println("chmodFile file fail");
            return false;
        }
    }

    public static boolean deleteFile(String dest, SFTPClient sftpClient){
        try {
            sftpClient.rm(dest);
            return true;
        } catch (IOException e){
            return false;
        }
    }

    public static String execCommand(String command, SSHClient sshClient,String rootPass){
        Session session = null;
        try {
            session = sshClient.startSession();
            String rootCommand = "echo " + rootPass + " | sudo -S " + command;
            Session.Command cmd = session.exec(rootCommand);
            String errorResult = IOUtils.readFully(cmd.getErrorStream()).toString();
            String normalResult = IOUtils.readFully(cmd.getInputStream()).toString();
            Integer exitStatus = cmd.getExitStatus();
            if (exitStatus == 1){
                if (errorResult.contains("Permission denied")){
                    System.out.println("用户权限不足");
                }
                return errorResult;
            } else {
                return normalResult;
            }
//            return IOUtils.readFully(cmd.getErrorStream()).toString();
        } catch (IOException e){
            System.out.println("execCommand fail");
            return null;
        } finally {
            if (session != null){
                try {
                    session.close();
                } catch (IOException e){
                    System.out.println("execCommand close session fail");
                    return null;
                }
            }
        }
    }

}
